// 美陪服务管理

var constant = require('../../const');
var models = require('../../models');
var fs = require('fs');
var AV = require('leanengine');
var multiparty = require('multiparty');


function getServer(req, res) {
	if (!req.AV.user) {
		res.redirect('/admin');
	}

	// 0是未上架, 1 已经上架的
	var query = new AV.Query(models.MPServer);
	query.descending("priority");
	query.find({
		success: function(servers) {
			var data = {
				"username": req.session.username,
				"avatar": req.session.useravatar,
				"servers": servers
			};
			res.render('admin/server/index', data);
		},
		error: function(error) {
			res.render('admin/server/index', {
				"username": req.session.username,
				"avatar": req.session.useravatar
			});
		}
	});
}

//server 编辑页面
function getServerEdit(req, res) {
	if (!req.AV.user) {
		res.redirect('/admin');
	}

	var objectId = req.query.objectId;
	// 编辑专题
	var serverQuery = new AV.Query(models.MPServer);
	serverQuery.get(objectId, {
		success: function(server) {
			var data = {
				"username": req.session.username,
				"avatar": req.session.useravatar,
				"server": server,
			};
			res.render('admin/server/edit', data);
		},
		error: function(error) {
			res.redirect("/admin/server");
		}
	});
}

//serer 修改
function serverEdit(req, res) {
	if (!req.AV.user) {
		res.redirect('/admin');
	}

	var form = new multiparty.Form();
	form.parse(req, function(err, fields, files) {
		var serverImg = files.serverImg[0];
		var content = fields.content.toString();
		var priority = fields.priority;
		var type = fields.type;
		var price = fields.price;
		var isNew = fields.isNew;
		var objectId = fields.objectId;
		var area = fields.area.toString().split(",");

		if (serverImg.size !== 0) {
			fs.readFile(serverImg.path, function(err, data) {
				if (err) {
					var data = {
						"username": req.session.username,
						"avatar": req.session.useravatar,
						"errMsg": "图片保存失败, 请重试",
					};
					res.render('admin/server/edit', data);
				}
				var base64Data = data.toString('base64');
				var theFile = new AV.File(serverImg.originalFilename, {
					base64: base64Data
				});
				theFile.save().then(function(theFile) {
					// 文件保存成功, 新增服务
					var server = new models.MPServer;
					server.set("objectId", objectId);
					server.set("content", content);
					server.set("status", 0);
					server.set("priority", parseInt(priority));
					server.set("price", parseInt(price));
					server.set("type", parseInt(type));
					server.set("isNew", parseInt(isNew));
					server.set("img", theFile);
					server.set("area", area);
					server.save({
						success: function() {
							res.redirect('/admin/server');
						},
						error: function(server, error) {
							console.log(error);
							//res.redirect('/admin/server');
						}
					});
				});
			});
		} else {
			var server = new models.MPServer;
			server.set("objectId", objectId);
			server.set("content", content);
			server.set("status", 0);
			server.set("priority", parseInt(priority));
			server.set("price", parseInt(price));
			server.set("type", parseInt(type));
			server.set("isNew", parseInt(isNew));
			server.set("area", area);
			server.save({
				success: function() {
					res.redirect('/admin/server');
				},
				error: function(server, error) {
					console.log(error);
					//res.redirect('/admin/server');
				}
			});
		}
	});
}

//server 添加页面
function getServerAdd(req, res) {
	if (!req.AV.user) {
		res.redirect('/admin');
	}

	// 新增服务
	var data = {
		"username": req.session.username,
		"avatar": req.session.useravatar
	};
	res.render('admin/server/add', data);
}

//server 添加
function serverAdd(req, res) {
	if (!req.AV.user) {
		res.redirect('/admin');
	}

	var form = new multiparty.Form();
	form.parse(req, function(err, fields, files) {
		var serverImg = files.serverImg[0];
		var content = fields.content.toString();
		var priority = fields.priority;
		var type = fields.type;
		var price = fields.price;
		var isNew = fields.isNew;
		var area = fields.area.toString().split(",");

		if (serverImg.size !== 0) {
			fs.readFile(serverImg.path, function(err, data) {
				if (err) {
					var data = {
						"username": req.session.username,
						"avatar": req.session.useravatar,
						"errMsg": "图片保存失败, 请重试",
					};
					res.render('admin/server/add', data);
				}
				var base64Data = data.toString('base64');
				var theFile = new AV.File(serverImg.originalFilename, {
					base64: base64Data
				});
				theFile.save().then(function(theFile) {
					// 文件保存成功, 新增服务
					var server = new models.MPServer;
					server.set("content", content);
					server.set("status", 0);
					server.set("priority", parseInt(priority));
					server.set("price", parseInt(price));
					server.set("type", parseInt(type));
					server.set("isNew", parseInt(isNew));
					server.set("img", theFile);
					server.set("area", area);
					server.save({
						success: function() {
							res.redirect('/admin/server');
						},
						error: function(server, error) {
							console.log(error);
							//res.redirect('/admin/server');
						}
					});
				});
			});
		} else {
			var data = {
				"username": req.session.username,
				"avatar": req.session.useravatar,
				"errMsg": "请选择文件",
			};
			res.render('admin/server/add', data);
		}
	});
}

//server 发布
function publishServer(req, res) {
	if (!req.AV.user) {
		res.redirect('/admin');
	}

	var objectId = req.body.objectId;
	var serverQuery = new AV.Query(models.MPServer);
	serverQuery.get(objectId, {
		success: function(server) {
			server.set("status", 1);
			server.save().then(function(obj) {
				res.send({
					"code": 0,
					"msg": "成功"
				});
			});
		},
		error: function(error) {
			res.send({
				"code": 1,
				"msg": "失败"
			});
		}
	});

}

//server 下架
function disableServer(req, res) {
	if (!req.AV.user) {
		res.redirect('/admin');
	}

	var objectId = req.body.objectId;
	var serverQuery = new AV.Query(models.MPServer);
	serverQuery.get(objectId, {
		success: function(server) {
			server.set("status", 0);
			server.save().then(function(obj) {
				res.send({
					"code": 0,
					"msg": "成功"
				});
			});
		},
		error: function(error) {
			res.send({
				"code": 1,
				"msg": "失败"
			});
		}
	});

}

//server 删除
function deleteServer(req, res) {
	if (!req.AV.user) {
		res.redirect('/admin');
	}

	var objectId = req.body.objectId;
	var serverQuery = new AV.Query(models.MPServer);
	serverQuery.get(objectId, {
		success: function(server) {
			server.destroy().then(function(obj) {
				res.send({
					"code": 0,
					"msg": "成功"
				});
			});
		},
		error: function(error) {
			res.send({
				"code": 1,
				"msg": "失败"
			});
		}
	});

}


exports.getServer = getServer
exports.getServerEdit = getServerEdit;
exports.serverEdit = serverEdit;
exports.getServerAdd = getServerAdd;
exports.serverAdd = serverAdd;
exports.publishServer = publishServer;
exports.disableServer = disableServer;
exports.deleteServer = deleteServer;